package com.wechart.controller;

import com.wechart.common.Const;
import com.wechart.dao.domain.Netresult;
import com.wechart.dao.domain.Orgnetinfo;
import com.wechart.model.NetResultPrint;
import com.wechart.model.OrgNetInfoPrint;
import com.wechart.model.SelectModel;
import com.wechart.model.StatisticsModel;
import com.wechart.service.manage.OrginfoService;
import com.wechart.service.statistics.NetresultService;
import com.wechart.service.statistics.OrgnetinfoService;
import com.wechart.utils.CsvWriter;
import com.wechart.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by silenceholic on 2017/5/8.
 */
@Controller
@RequestMapping("statistics/")
public class StatisticsController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(StatisticsController.class);

    @Resource(name="netresultService")
    private NetresultService netresultService;

    @Resource(name="orgnetinfoService")
    private OrgnetinfoService orgnetinfoService;

    @Resource(name="orginfoService")
    private OrginfoService orginfoService;

    /**
     * 轧差结果信息
     * @param statisticsModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "netresult.htm" )
    public ModelAndView getNetresult(@ModelAttribute StatisticsModel statisticsModel){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Netresult> data = netresultService.getNetresult(statisticsModel);
        map.put("data",data);
        map.put("recordsTotal", data.size());
        map.put("recordsFiltered", data.size());
        map.put("start",statisticsModel.getStart());
        map.put("end",statisticsModel.getEnd());
        map.put("code",statisticsModel.getCode());
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("statistics/netresult");
        return mv;
    }

    /**
     * 轧差结果信息打印
     * @return
     */
    @RequestMapping(value = "netresult/print.htm")
    public ModelAndView netresultPrint(){
            Map<String, Object> map = new HashMap<String, Object>();
            List<NetResultPrint> data = netresultService.netResultPrints();
            map.put("data",data);
            ModelAndView mv = super.modelAndView(map);
            mv.setViewName("statistics/netresultprint");
            return mv;
    }

//    @RequestMapping(value = "orgnetinfo.htm")
//    public String allorgnetinfo(){
//        return "statistics/orgnetinfo";
//    }

    /**
     * 机构汇总轧差信息
     * @param statisticsModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "orgnetinfo.htm" )
    public ModelAndView getOrgnetinfo(@ModelAttribute StatisticsModel statisticsModel, HttpSession session){
        Map<String, Object> map = new HashMap<String, Object>();
        String areacode = ((Auth)session.getAttribute(Const.SESSION_USER)).getUser().getAreacode();
        statisticsModel.setOrgcodelist(orginfoService.getOrgcodeByAreacode(areacode));
        statisticsModel.setAreacode(areacode);
        List<Orgnetinfo> data = orgnetinfoService.getOrgnetinfo(statisticsModel);
        map.put("data",data);
        map.put("recordsTotal", data.size());
        map.put("recordsFiltered", data.size());
        map.put("start",statisticsModel.getStart());
        map.put("end",statisticsModel.getEnd());
        map.put("code",statisticsModel.getCode());
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("statistics/orgnetinfo");
        return mv;
    }

    /**
     * 轧差结果信息打印
     * @return
     */
    @RequestMapping(value = "orgnetinfo/print.htm")
    public ModelAndView orgnetinfoPrint(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<OrgNetInfoPrint> data = orgnetinfoService.orgnetInfoPrints();
        map.put("data",data);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("statistics/orgnetinfoprint");
        return mv;
    }


    /**
     * 轧差结果下载
     * @param statisticsModel
     * @return
     */
    @RequestMapping(value = "downloadNetresult.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadNetresultTables(@ModelAttribute StatisticsModel statisticsModel)
            throws Exception{

        List<Netresult> data = netresultService.getNetresult(statisticsModel);
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }

    /**
     * 机构汇总轧差信息
     * @param statisticsModel
     * @return
     */
    @RequestMapping(value = "downloadOrgnetinfo.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadOrgnetinfoTables(@ModelAttribute StatisticsModel statisticsModel, HttpSession session)
            throws Exception{
        String areacode = ((Auth)session.getAttribute(Const.SESSION_USER)).getUser().getAreacode();
        statisticsModel.setOrgcodelist(orginfoService.getOrgcodeByAreacode(areacode));
        statisticsModel.setAreacode(areacode);
        List<Orgnetinfo> data = orgnetinfoService.getOrgnetinfo(statisticsModel);
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }

    /**
     * 测试图表
     * @return
     */
    @RequestMapping(value = "testChart.htm")
    public ModelAndView testChart(){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("test","test");
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("statistics/testChart");
        return mv;
    }

    /**
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "select/selectChart.do")
    public Object selectChart(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<SelectModel> list=new ArrayList<SelectModel>();
        list.add(new SelectModel("衣服","23"));
        list.add(new SelectModel("裤子","45"));
        list.add(new SelectModel("帽子","99"));
        list.add(new SelectModel("袜子","32"));
//        map.put("data",list);
//        ModelAndView mv = super.modelAndView(map);
//        mv.setViewName("statistics/testChart");
        return list;
    }



}
